package top.dvvj.wheelmall.common.util;

import lombok.Data;
import top.dvvj.wheelmall.common.enums.CodeEnum;

import java.io.Serializable;

/**
 * 通用响应消息主体
 *
 * @author Dvj
 * @date 2021/9/17 15:53
 */
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 默认成功响应状态码
     */
    public static final int SUCCESS = CodeEnum.SUCCESS.getCode();
    /**
     * 默认失败响应状态码
     */
    public static final int FAIL = CodeEnum.UNKNOWN_EXCEPTION.getCode();
    /**
     * 响应状态码
     */
    private Integer code;
    /**
     * 提示信息
     */
    private String msg;
    /**
     * 响应数据
     */
    private T data;

    public static <T> R<T> ok() {
        return restResult(SUCCESS, "", null);
    }


    public static <T> R<T> ok(T data) {
        return restResult(SUCCESS, "", data);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(SUCCESS, msg, data);
    }

    public static <T> R<T> fail() {
        return restResult(FAIL, "", null);
    }

    public static <T> R<T> fail(String msg) {
        return restResult(FAIL, msg, null);
    }

    public static <T> R<T> fail(Integer code, String msg) {
        return restResult(code, msg, null);
    }

    public static <T> R<T> fail(CodeEnum codeEnum) {
        return restResult(codeEnum.getCode(), codeEnum.getMsg(), null);
    }

    private static <T> R<T> restResult(Integer code, String msg, T data) {
        return new R(code, msg, data);
    }

    private R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}
